/*-
 * Copyright (c) 2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ NUMTK: sfkrm.c    Rev. 0.0.1    2013-09-23T14:32+08:00 $
 */

#include <stdlib.h>
#include <stdio.h>

#include <bscerr.h>

#include <numtk.h>


int rsfd_init(size_t rz, size_t cz, size_t kz, rsfd_t *rsfd)
{
    int ierr = ERRNO_UNDEFINED;

    rsfd->rz = rz;
    rsfd->cz = cz;

    rsfd->kz = kz;

    rsfd->rptr = (uidx*) calloc(rsfd->rz * 2, sizeof(uidx_t));

    rsfd->elst = (fltd_t*) calloc(rsfd->kz, sizeof(fltd_t));

    return ierr;
}


int rsfd_dest(rsfd_t *rsfd)
{
    int ierr = ERRNO_UNDEFINED;

    free(rsfd->rptr);
    free(rsfd->elst);

    return ierr;
}


int rsfd_stdout(rsfd_t *rsfd)
{
    int ierr = ERRNO_UNDEFINED;
    size_t zi;
    uidx_t ui;

    for (zi = 0; zi < rsfd->rz; zi++)
    {
        for (ui = rsfd->rptr[zi * 2]; ui < rsfd->rptr[zi * 2 + 1]; ui++)
        {
            printf("(%7zd, %7u): % .15E\n", zi, rsfd->elst[ui].index, rsfd->elst[ui].value);
        }
    }

    return ierr;
}
